<?php
class GameAction extends CommonAction {
	protected $connection = array(
			'db_type'  => 'mongo',
			'db_user'  => '',
			'db_pwd'   => '',
			'db_host'  => '192.168.1.220',
			'db_port'  => '27017',
			'db_name'  => 'ygzj_dev'
	);
	function index()
	{
		$users	=	explode(":", gameInfo("online_count"));
		$info = array(
				'游戏地址'=>C("GAME_IP"),
				'端口'=>C("GAME_PORT"),
				'当前在线'=>$users[1],
				'节点信息'=>gameInfo("get_node_status"),
				'更新城战'=>"<a href='".U('Game/refreshBattle','')."'>更新</a>",
				'执行时间限制'=>ini_get('max_execution_time').'秒',
		);
		$this->assign('info',$info);
		$this->display();
	}
	/**
	 * 发布公告
	 */
	function broadcast()
	{
		if ($_GET["content"]) {
			gameInfo("broadmsg",unescape($_GET["content"]));
			echo unescape($_GET["content"]);
			exit;
		}
		$info = array(
				'公告内容'=>"<textarea name='content' id='content' rows=6 cols=50></textarea>",
				'操作'=>"<a href='javascript:onbroad();'>发布</a>",
		);
		$this->assign('info',$info);
		$this->display();
	}
	
	/**
	 * 物品相关
	 */
	function goods()
	{
		$this->display();
	}
	/**
	 * 查看充值
	 */
	function charge()
	{
		$Model	=	new MongoModel("ygzj_dev.log_pay",null,"mongo");
		$result	=	$Model->field("player_id,nickname,lv,payitem,pay_time,pay_gold")->select();
		$info	=	array();
		$payTotal	=	0;
		$realTotal=0;
		foreach ($result as $key=>$val)
		{
			$tmpItem	=	array();
			$payInfo	=	explode("*", $val["payitem"]);
			$tmpItem['player_id']=$val["player_id"];
			$tmpItem['goods_id']=$payInfo[0];
			$tmpItem['goods_price']=$payInfo[1];
			$tmpItem['goods_num']=$payInfo[2];
			$tmpItem['payitem']=$val["payitem"];
			$tmpItem['pay_gold']=$val["pay_gold"];
			$tmpItem['pay_time']=$val["pay_time"];
			$tmpItem['lv']=$val["lv"];
			$tmpItem['nickname']=$val["nickname"];
			array_push($info, $tmpItem);
			$payTotal	+= $payInfo[1]*$payInfo[2];
			$realTotal += $tmpItem['pay_gold'];
		}
		$info	=	$this->array_sort($info,"pay_time");
		$this->assign('info',$info);
		$this->assign('payTotal',$payTotal);
		$this->assign('realTotal',$realTotal);
		$this->display();
	}
	
	function array_sort($arr,$keys,$type='desc'){
		$keysvalue = $new_array = array();
		foreach ($arr as $k=>$v){
			$keysvalue[$k] = $v[$keys];
		}
		if($type == 'asc'){
			asort($keysvalue);
		}else{
			arsort($keysvalue);
		}
		reset($keysvalue);
		foreach ($keysvalue as $k=>$v){
			$new_array[$k] = $arr[$k];
		}
		return $new_array;
	}
	
	
	/**
	 * 查询游戏相关
	 */
	function query()
	{
		if($_GET["type"])
		{
			switch ($_GET["type"])
			{
				case "user":
					$Model	=	new MongoModel("ygzj_dev.player",null,"mongo");
					$map["nickname"]	=	unescape($_GET["name"]);
					$result	=	$Model->field("id")->where($map)->select();
					foreach ($result as $key=>$val)
					{
						echo $val["id"];
						break;
					}
					break;
			}
		}
	}
	
	function addGold()
	{
		$userid	=	$_GET["user"];
		$type	=	$_GET["type"];
		$count	=	$_GET["count"];
		//array("add","sub")
		//先向数据库里加
		//通知玩家
		//echo "{$userid}={$type}_add_{$count}_gm";exit;
		$res	=	gameInfo("notice_change_money","{$userid}={$type}_add_{$count}_1");
		echo $res;
	}
	
	function mail()
	{
		$this->display();
	}
	
	function sendmail()
	{
		//1,529,袁嘉盛,4,1
		//角色名 物品ID 强化等级 标题 内容 金币 元宝 是否绑定 是否交易
		//192.168.1.220:8777/send_gm_mail?r=529&gt=1&gi=10218&th=erww&ch=fdsaerw&coin=12&gold=22&bind=5&trade=2
		//print_r($_GET);exit;
		$requestStr	=	"";
		foreach ($_GET as $key=>$val)
		{
			if($key=="_URL_")
			{
				continue;
			}
			if($requestStr!="")
			{
				$requestStr	.="&";
			}
			$requestStr .= $key."=".$val;
		}
		$res	=	gameInfo("send_gm_mail",$requestStr);
		echo $res;
	}
	
	/**
	 * 用户管理
	 */
	function usermgr()
	{
		$this->display();
	}
	
	/**
	 * 设置禁言
	 */
	function donttalk()
	{
		$type	=	$_GET["type"];
		$uid	=	$_GET["uid"];
		$res	=	gameInfo("donttalk",$uid."=".$type);
		echo $res;
	}
	/**
	 * 踢人下线
	 */
	function kickUser()
	{
		$uid	=	$_GET["uid"];
		$res	=	gameInfo("kickUser",$uid);
		echo $res;
	}
	/**
	 * 封角色
	 */
	function banUser()
	{
		$uid	=	$_GET["uid"];
		$type	=	$_GET["type"];
		$res	=	gameInfo("banrole",$uid."=".$type);
		echo $res;
	}
	/**
	 * 创建角色 action
	 */
	function newac()
	{
		$this->display();
	}
	
	/**
	 * 提交用户创建
	 */
	function createUser()
	{
		$Model	=	new MongoModel("ygzj_dev.player",null,"mongo");
		$accid = intval(urldecode($_REQUEST['accid']));
		$accname = urldecode($_REQUEST['accname']);
		$sn = intval($_REQUEST['sn']);
		$realm = 100;
		$career = intval($_REQUEST['career']);
		$sex = intval($_REQUEST['sex']);
		$name = urldecode($_REQUEST['name']);
		$tstamp	=	time();
// 		echo $accid."<br/>";
// 		echo $accname."<br/>";
// 		echo $sn."<br/>";
// 		echo $realm."<br/>";
// 		echo $career."<br/>";
// 		echo $sex."<br/>";
// 		echo $name."<br/>";
// 		echo $tstamp."<br/>";
// 		exit;
		if($accid > 0 && !empty($accname) && $sn > 0 && $realm > 0 && $career > 0 && $sex > 0 && !empty($name) && $tstamp > 0)
		{
			$Model	=	new MongoModel("ygzj_dev.base_career",null,"mongo");
			//基础参数验证
			$ticket = $this->_createTicket($accid, $accname, $tstamp);
			//检查是否已有角色名
			$exists = $this->check_nickname_exists($name);
			if($exists) exit("-6");
			//检查是否已经注册
			$reged = $this->check_account_reged($accname,$sn);
			if($reged) exit("0");
			$base_career_info = $Model->where(array("career_id"=>$career))->find();
			$sceneid = 100;
			//初始化坐标
			switch($career)
			{
				case 1:$x=21;$y=13;break;
				case 2:$x=12;$y=28;break;
				case 3:$x=30;$y=18;break;
				case 4:$x=18;$y=33;break;
				case 5:$x=12;$y=17;break;
			}
			$att_area = $base_career_info["init_att_area"];
			$speed = $base_career_info["init_speed"];
			$spirit = $base_career_info["init_spirit"];
			$gold = $base_career_info["init_gold"];
			$cash = $base_career_info["init_cash"];
			$coin = $base_career_info["init_coin"];
			$forza = $base_career_info["forza"];
			$physique = $base_career_info["physique"];
			$agile = $base_career_info["agile"];
			$wit = $base_career_info["wit"];
			$att_speed = $base_career_info["att_speed"];
			$time = time();
			$cellnum = 36;
			$storenum = 36;
			$lv = 1;
			$hp = round($base_career_info["hp_init"] + $base_career_info["hp_physique"] * $physique + $base_career_info["hp_lv"] * $lv);
			$mp = 100;//round($base_career_info["mp_init"] + $base_career_info["mp_wit"] * $wit + $base_career_info["mp_lv"] * $lv);
			//$quickbar = "[{6,3,3},{5,3,2},{4,3,1}]";
			$realm_honor = 100 ;
			
			//$Model	=	new MongoModel("ygzj_dev.player",null,"mongo");
			$p_id = intval(time()/rand(4, 999));
			$fields = array(
				"id" => $p_id,
				"accid" => $accid ,
				"accname" => $accname, 
				"sn" => $sn,
				"lv" => $lv,
				"career" => $career,
				"realm" => $realm,
				"sex" => $sex,
				"nickname" => $name,
				"reg_time" => $time,
				"last_login_time" => 0,
				"x" => $x,
				"y" => $y,
				"scene" => $sceneid ,
				"forza" => $forza,
				"physique" => $physique ,
				"agile" => $agile,
				"wit" => $wit,
				"coin" => $coin,
				"bcoin" => 0,
				"cash" => $cash,
				"gold" => $gold,
				"hp" => $hp,
				"mp" => $mp,
				"spirit" => $spirit,
				"att_area" => $att_area,
				"att_speed" => $att_speed,
				"speed" => $speed,
				"cell_num" => $cellnum,
				"store_num" => $storenum,
				"other" => 0,
				//"quickbar" => $quickbar,
				"realm_honor" => $realm_honor
			);
			$Model	=	new MongoModel("ygzj_dev.player",null,"mongo");
			$Model ->add($fields);
			exit("$p_id");
			
		}else
		{
			exit("0");
		}
	}
	/**
	 * 查询列表与状态
	 */
	function listStatus()
	{
		$this->display();
	}
	
	/**
	 * 生成登陆串
	 */
	function genurl()
	{
		$loginstr	=	"";
		$Model	=	new MongoModel("ygzj_dev.player",null,"mongo");
		$map["nickname"]	=	unescape($_GET["name"]);
		$result	=	$Model->where($map)->find();
		if($result["id"]>0)
		{
			$ltime	=	time();
			$loginstr .= "r=1&accid=" . $result ["accid"];
			$loginstr .= "&accname=" . $result ["accname"];
			$loginstr .= "&sn=" . $result ["sn"];
			$loginstr .= "&ticket=" . $this->_createTicket($result ["accid"], $result ["accname"], $ltime);
			$loginstr .= "&tstamp=" . $ltime;
		}
		else
		{
			$loginstr	=	"用户不存在！";
		}
		echo $loginstr;
	}
	
	function refreshBattle()
	{
		$res	=	gameInfo("load_base_data");
		$this->assign('jumpUrl', U('Game/index'));
		$this->success('更新:'.$res);
	}
	
	//创建验证窜
	function _createTicket($accid, $accname, $tstamp, $subfix = "THISIS407FAIRYTASK")
	{
		$ticket = md5($accid . $accname . $tstamp. $subfix);
		return $ticket;
	}
	
	//检查角色名是否重复
	function check_nickname_exists($nickname)
	{
		$Model	=	new MongoModel("ygzj_dev.player",null,"mongo");
		$result	=	$Model->where(array("nickname"=>$nickname))->find();
		if($result["id"] > 0)
		{
			return true;
		}
		return false;
	}
	//检查帐号是否已经注册
	function check_account_reged($accname,$sn)
	{
		$Model	=	new MongoModel("ygzj_dev.player",null,"mongo");
		$result	=	$Model->field("id")->where(array("accname"=>$accname,"sn"=>$sn))->find();
		if($result["id"] > 0)
		{
			return true;
		}
		return false;
	}
}

?>